<template>
    <el-container>
        <!-- 头部 -->
        <el-header>
            <div class="logo">
                <img src="../assets/heima.png">
                <span>电商后台管理系统</span>
            </div>
            <el-button @click="loginOut" type="info">退出</el-button>
        </el-header>
        <el-container>
            <!-- 侧边栏 -->
            <el-aside width="isCollapse == true ? '64px' : '200px'">
                <!-- 折叠按钮 -->
                <div @click="setFlexMenu" class="flex_menu">|||</div>
                <el-menu
                router
                :collapse="isCollapse"
                unique-opened
                :collapse-transition="false"
                :default-active="currentActive"
                background-color="#333844"
                text-color="#fff"
                active-text-color="#2b73da">
                    <el-submenu :index="item1.id + ''" :key="item1.id" v-for="(item1) in menusList">
                        <!-- 一级菜单栏 -->
                        <template slot="title">
                            <i :class="menusIcon[item1.id]"></i>
                            <span>{{item1.authName}}</span>
                        </template>
                        <!-- 二级菜单栏 -->
                        <el-menu-item-group>
                            <el-menu-item @click="getCurrentActive(item2.path)" :index="'/' + item2.path" :key="item2.id" v-for="(item2) in item1.children">
                                <i class="el-icon-menu"></i>
                                {{item2.authName}}
                            </el-menu-item>
                        </el-menu-item-group>
                    </el-submenu>
                </el-menu>
            </el-aside>
            <!-- 侧边栏内容区域 -->
            <el-main><router-view></router-view></el-main>
        </el-container>
    </el-container>
</template>

<script>
export default {
    data() {
        return {
            // 存储左侧菜单栏列表
            menusList: [],
            // 记录被点击的菜单项
            currentActive: '',
            // 折叠菜单栏开关
            isCollapse: false,
            // 菜单栏图标
            menusIcon: {
                125: 'iconfont icon-users',
                103: 'iconfont icon-tijikongjian',
                101: 'iconfont icon-shangpin',
                102: 'iconfont icon-danju',
                145: 'iconfont icon-baobiao'
            }
        }
    },
    created() {
        this.getMenusList();
        this.currentActive = window.sessionStorage.getItem('active');
    },
    methods: {
        // 退出功能
        loginOut() {
            window.sessionStorage.clear();
            this.$router.push('/login');
        },
        // 获取左侧菜单栏列表
        async getMenusList() {
            const { data: res } = await this.$http.get('menus');
            if (res.meta.status != 200) return this.$message.error('获取左侧菜单栏列表失败');
            this.menusList = res.data;
        },
        // 获取被点击的菜单项
        getCurrentActive(path) {
            this.currentActive = '/' + path;
            window.sessionStorage.setItem('active', this.currentActive);
        },
        // 设置折叠菜单栏
        setFlexMenu() {
            this.isCollapse = !this.isCollapse;
        }
    }
}
</script>

<style lang="less" scoped>
.el-container {
    height: 100%;
}
.el-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: #373e40;
}
.el-aside {
    background-color: #333844;
}
.el-main {
    background-color: #e6eaec;
}
.logo img {
    vertical-align: middle;
}
.logo span {
    margin-left: 20px;
    font-size: 20px;
    font-weight: 700;
    color: #fff;
}
.el-menu {
    border-right: 0;
}
.flex_menu {
    height: 30px;
    line-height: 30px;
    text-align: center;
    color: #fff;
    letter-spacing: 2px;
    cursor: pointer;
}
.iconfont {
    margin-right: 20px;
}
</style>